package ch.zhaw.cctd.domain.prototype;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Representiert ein MapElementTyp und dessen Einstellungen im XML Config File
 * @author Rolf Koch
 * @author Benjamin Felder
 */
public class MapElementPrototype extends Prototype implements Serializable {

	private static final long serialVersionUID = 4612629003035947506L;

	public static List<MapElementPrototype> generateMapElementPrototypes(Map<String,String> data) {
		List<MapElementPrototype> prototypes = new ArrayList<MapElementPrototype>();
		MapElementPrototype dummy = new MapElementPrototype(data, "", "");
		String[] types = new String[] {
				"mapelements.background", 
				"mapelements.constructionzone", 
				"mapelements.stonewall"
		};
		for (String type : types) {
			String value = dummy.getValue(type);
			if (value == null) { // only one
				prototypes.add(new MapElementPrototype(data, type, type+"."));
			} else { // multiple
				Integer size = Integer.parseInt(value);
				for (int i = 0;i < size;i++) {
					prototypes.add(new MapElementPrototype(data, type, type+"."+i+"."));
				}
			}
		}
		return prototypes;
	}
	
	public MapElementPrototype(Map<String,String> data, String type, String prefix) {
		super(data);
		this.prefix = prefix;
		this.type = type;
	}
	
	private final String type;
	private final String prefix;
	
	public boolean isBackground() {
		return "mapelements.background".equals(this.type);
	}
	
	public boolean isConstructionZone() {
		return "mapelements.constructionzone".equals(type);
	}
	
	public boolean isStonewall() {
		return "mapelements.stonewall".equals(type);
	}

	public int getXX() {
		return this.getIntegerValue(prefix+"xx");
	}

	public int getXY() {
		return this.getIntegerValue(prefix+"xy");
	}

	public int getYY() {
		return this.getIntegerValue(prefix+"yy");
	}

	public int getYX() {
		return this.getIntegerValue(prefix+"yx");
	}
	
	public String getTextureId() {
		return this.getValue(prefix+"texture.refid");
	}
	
	public int getLayer() {
		return this.getIntegerValue(prefix+"layer");
	}
	
	public Integer getPlayerid() {
		return this.getIntegerValue(prefix+"player.refid");
	}
	
}
